package com.jimmy.medical.service.impl;

import com.jimmy.medical.base.impl.JpaSpecificationServiceImpl;
import com.jimmy.medical.entity.*;
import com.jimmy.medical.repository.*;
import com.jimmy.medical.service.ISystemService;
import org.apache.commons.collections.IteratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemServiceImpl extends JpaSpecificationServiceImpl<HospitalRepository, HospitalRepository, Hospital, Long>
        implements ISystemService {

    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private DepartmentRepository departmentRepository;
    @Autowired
    private DoctorRepository doctorRepository;
    @Autowired
    private MeasuresRepository measuresRepository;
    @Autowired
    private MedicineRepository medicineRepository;
    @Autowired
    private DiseaseDicRepository diseaseDicRepository;
    @Autowired
    private SysCodeRepository sysCodeRepository;
    @Autowired
    private MemberTypeRepository memberTypeRepository;
    @Autowired
    private ServiceItemRepository serviceItemRepository;


    @Override
    public List<SysCode> getCityList() {
        return sysCodeRepository.findByCodeSortAndIsShowApp("city", "1");
    }

    @Override
    public List<Hospital> getAllHospitalList(Long cityId) {
        if(cityId == null || cityId == 0)
            return IteratorUtils.toList(hospitalRepository.findAll().iterator());
        return hospitalRepository.findByCityId(cityId);
    }

    @Override
    public List<SysCode> getServiceTypeList() {
        return sysCodeRepository.findByCodeSort("serviceType");
    }

    @Override
    public List<MemberType> getMemberTypeList(Long serviceTypeId) {
        return memberTypeRepository.findByServiceType(serviceTypeId);
    }

    @Override
    public List<ServiceItem> getServiceItemList(String serviceFlow) {
        return serviceItemRepository.findByServiceFlow(serviceFlow);
    }

    @Override
    public List<HospitalDepartment> getDepartments(Long hospitalId) {
        return departmentRepository.findHospitalDepartmentByHospital(hospitalId);
    }

    @Override
    public List<Doctor> getDoctors(Long departmentId) {
        return doctorRepository.findDoctorByDepartment(departmentId);
    }

    @Override
    public List<SysCode> getMeasureTypeList() {
        return sysCodeRepository.findByCodeSortAndIsShowApp("指标分类","1");
    }

    @Override
    public List<Measures> getAllMeasureList() {
        return IteratorUtils.toList(measuresRepository.findAll().iterator());
    }

    @Override
    public List<Measures> getAllPhysicalMeasureList() {
        return measuresRepository.findBySortsIsNotNull();
    }



    @Override
    public List<Measures> getMeasureList(String firstLetter) {
        return measuresRepository.findByFirstLetterLike(firstLetter.toUpperCase()+"%");
    }

    @Override
    public List<Medicines> getMedicineList(String firstLetter) {
        return medicineRepository.findByFirstLetterLike(firstLetter.toUpperCase()+"%");
    }

    @Override
    public List<DiseaseDic> getDiseaseList(String firstLetter) {
        return diseaseDicRepository.findByFirstLetterLike(firstLetter.toUpperCase()+"%");
    }
}